<?php
//GET Request
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    ?>
<form id="new-comment-form" action="<?php echo Utils::getUrl('comment/new.php') ?>" method="post" >
        <div>
            <input name="id" type="hidden" value="<?php echo $id; ?>" />
            <input name="commentType" type="hidden" value="<?php echo $commentType; ?>" />
        </div>
        <div class="form-element" >
            <label for="text">New comment</label>
                <textarea name="text" id="new-comment-text" onkeypress="return imposeMaxLength(this, 512);" rows="8" cols="64" ></textarea>
        </div>
        <div class="submit" >
            <input type="submit" value="Send" />
        </div>
    </form>
    <script type="text/javascript">
        $("#new-comment-form").submit(function(){
            var form = this;
            if(validateCommentForm(form)){
                submitAndRefreshComment(form);
            }
            return false;
        });
    </script>

    <?php
}
//POST Request
else if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    require_once ('..' . DIRECTORY_SEPARATOR . 'initialize.php');
    include_once(Utils::getPath(array('repositories', 'solutionRepository.php')));

    $text = isset($_POST['text']) ? Utils::cleanDoubleSpaces(htmlspecialchars($_POST['text'])) : null;
    $id = isset($_POST['id']) ? $_POST['id'] : null;
    $commentType = isset($_POST['commentType']) ? $_POST['commentType'] : null;

    if (!isset($text) || $text == '') {
        if (!$request_ajax)
            Utils::showError('Comment is empty');
    } else if (strlen($text) > 512) {
        if (!$request_ajax)
            Utils::showError('The maximum size of a comment is 512 characters');
    } else if ($id == null || $commentType == null) {
        Utils::goToErrorPage();
    } else {
        include_once(Utils::getPath(array('repositories', 'commentRepository.php')));
        //Update database
        $new_comment = new Comment();
        $new_comment->setIdAuthor($_SESSION['authenticatedId']);
        $new_comment->setText($text);
        if ($commentType == 1) {
            $new_comment->setIdChallenge($id);
            $new_comment->setIdType($commentType);
        } else if ($commentType == 2) {
            $solutionRep = new SolutionRepository();
            if(!$solutionRep->userCanAccessSolution($_SESSION['authenticatedId'], $solutionRep->getById($id)))
            {
                //If user can't access, usar can't comment
                Utils::goToErrorPage(StatusCodes::HTTP_FORBIDDEN);
            }
            $new_comment->setIdSolution($id);
            $new_comment->setIdType($commentType);
        }

        $commentRep = new CommentRepository();
        $new_comment = $commentRep->persist($new_comment);

        if ($new_comment == null) {
            Utils::goToErrorPage();
        }

        if (!$request_ajax)
            Utils::showInfo('Success!');
    }

    if (!$request_ajax) {
        //if the request isn't by ajax
        $referer = getenv("HTTP_REFERER");
        header("Location: $referer");
        exit();
    }
}
?>